package com.hundsun.documentripper.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hundsun.documentripper.domain.dto.BaseResponse;
import com.hundsun.documentripper.domain.dto.DocumentVersionView;
import com.hundsun.documentripper.domain.pojo.Document;
import com.hundsun.documentripper.domain.query.DocumentVersionQuery;
import com.hundsun.documentripper.error.ErrorCatcher;
import com.hundsun.documentripper.service.DocumentService;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：文档控制层
 * @date ：Created in 2021/1/26
 */
@Api("文档")
@RestController
public class DocumentController {

    @Autowired
    private DocumentService documentService;

    @ApiOperation("根据唯一标识获取文档对象")
    @GetMapping("/document/{uuid}")
    @ErrorCatcher(code = "5001")
    public BaseResponse<Document> getDocumentById(@PathVariable("uuid") String uuid) {
        return new BaseResponse<Document>(documentService.getDocumentById(uuid));
    }

    @ApiOperation("分页获取文档的版本列表")
    @PostMapping("/document/version")
    @ErrorCatcher(code = "5002")
    public BaseResponse<IPage<DocumentVersionView>> getDocumentVersionListByPage(@RequestBody DocumentVersionQuery documentVersionQuery) {
        return new BaseResponse<IPage<DocumentVersionView>>(documentService.getDocumentVersionListByPage(documentVersionQuery));
    }

    @ApiOperation("全量获取文档的版本列表")
    @PostMapping("/document/version/all")
    @ErrorCatcher(code = "5002")
    public BaseResponse<List<DocumentVersionView>> getDocumentVersionList(@RequestBody DocumentVersionQuery documentVersionQuery) {
        return new BaseResponse<List<DocumentVersionView>>(documentService.getDocumentVersionList(documentVersionQuery));
    }

    @ApiOperation("删除某个产品版本下的文档")
    @DeleteMapping("/productVersion/{productVersionUuid}/document/{uuid}")
    @ErrorCatcher(code = "5001")
    @Transactional
    public BaseResponse<Void> deleteDocumentByProductVersion(@PathVariable("productVersionUuid") String productVersionUuid,
                                                             @PathVariable("uuid") String uuid) {
        documentService.deleteDocumentVersionByProductVersion(productVersionUuid, uuid);
        return new BaseResponse<Void>();
    }

}